// TinyCCServer.cpp : Implementation of WinMain


#include "stdafx.h"
#include "resource.h"
#include "TinyCCServer.h"
#include "CCServer.h"
#include "ServerConfig.h"
#include "TinyServerDefine.h"

#include <stdio.h>

class CTinyCCServerModule : public CAtlServiceModuleT< CTinyCCServerModule, IDS_SERVICENAME >
{
public :
	DECLARE_LIBID(LIBID_TinyCCServerLib)
	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_TINYCCSERVER, "{35CD11FE-C1BA-480D-AE52-F3948D6E3200}")
	HRESULT InitializeSecurity() throw()
	{
		// TODO : Call CoInitializeSecurity and provide the appropriate security settings for 
		// your service
		// Suggested - PKT Level Authentication, 
		// Impersonation Level of RPC_C_IMP_LEVEL_IDENTIFY 
		// and an appropiate Non NULL Security Descriptor.

		return S_OK;
	}
public:
	HRESULT PreMessageLoop(int nShowCmd);
	CCCServer m_server;
public:
	HRESULT PostMessageLoop();
};

CTinyCCServerModule _AtlModule;



//
extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
                                LPTSTR /*lpCmdLine*/, int nShowCmd)
{
    return _AtlModule.WinMain(nShowCmd);
}


HRESULT CTinyCCServerModule::PreMessageLoop(int nShowCmd)
{
	// TODO: Add your specialized code here and/or call the base class
	m_status.dwControlsAccepted =m_status.dwControlsAccepted | SERVICE_ACCEPT_PAUSE_CONTINUE;
	HRESULT hr = __super::PreMessageLoop(nShowCmd);
	if (hr == S_FALSE) hr = S_OK;
	
	bool ret = m_server.init();

	if(ret == true)
		m_server.start();
	
	return hr;
}

HRESULT CTinyCCServerModule::PostMessageLoop()
{
	// TODO: Add your specialized code here and/or call the base class

	HRESULT hr = CAtlServiceModuleT<CTinyCCServerModule,100>::PostMessageLoop();

	g_Logger.log(debug1,"Server end");
	m_server.end();
	Sleep( 3000 );
	return hr;
}
